package org.dev.lms.controller;

import org.dev.auth.module.entity.SysUser;
import org.dev.auth.module.service.SysUserService;
import org.dev.lms.entity.Book;
import org.dev.lms.service.LibraryReaderService;
import org.dev.lms.entity.LibraryReader;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dev.common.core.page.PaginAtion;
import org.dev.common.core.result.ResponseResult;
import org.dev.lms.util.FlowCodeCons;
import org.dev.util.util.SequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.dev.common.core.aop.OperLog;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/libraryReader")
public class LibraryReaderController {
    @Autowired
    public LibraryReaderService libraryReaderService;

    @Autowired
    SequenceUtil sequenceUtil;

    @Autowired
    SysUserService sysUserService;

    /**
     * 保存和修改公用的
     *
     * @param libraryReader 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperLog(description = "libraryReader保存和修改")
    public ResponseResult<String> save(@RequestBody LibraryReader libraryReader) {
        boolean result = false;
        try {
            if (libraryReader.getId() != null) {
                if (StringUtils.isEmpty(libraryReader.getCardNo())) {
                    libraryReader.setCardNo(sequenceUtil.GeneratorCode(FlowCodeCons.READER_CAR_NO));
                }
                result = libraryReaderService.updateById(libraryReader);
            } else {
                libraryReader.setCardNo(sequenceUtil.GeneratorCode(FlowCodeCons.READER_CAR_NO));
                result = libraryReaderService.save(libraryReader);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }


    /**
     * 保存和修改公用的
     *
     * @param libraryReader 传递的实体
     * @return ResponseResult转换结果
     */
    @RequestMapping(value = "/reg-reader", method = RequestMethod.POST)
    @OperLog(description = "libraryReader保存和修改")
    public ResponseResult<String> regReader(@RequestBody LibraryReader libraryReader) {
        boolean result = false;
        try {
            libraryReader.setRegDate(LocalDateTime.now());
            libraryReader.setCardNo(sequenceUtil.GeneratorCode(FlowCodeCons.READER_CAR_NO));
            libraryReader.setStatus("0");
            result = libraryReaderService.save(libraryReader);
            //新增用户
            SysUser sysUser = new SysUser();
            sysUser.setLoginName(libraryReader.getPhone());

        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        if (result)
            return ResponseResult.success();
        return ResponseResult.error();
    }

    /**
     * 获取对象信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @OperLog(description = "libraryReader获取对象信息")
    public ResponseResult<LibraryReader> get(@RequestParam("id") Long id) {
        LibraryReader libraryReader = libraryReaderService.getById(id);
        return ResponseResult.success(libraryReader);
    }

    /**
     * 根据读者信息加载图书信息
     *
     * @param readerCardNo
     * @return
     */
    @GetMapping("/get-by-cardno")
    @OperLog(description = "book获取对象信息")
    public ResponseResult<LibraryReader> getByCardno(@RequestParam("readerCardNo") String readerCardNo) {
        LibraryReader libraryReader = new LibraryReader();
        libraryReader.setCardNo(readerCardNo);
        return ResponseResult.success(this.libraryReaderService.getOne(new QueryWrapper<>(libraryReader)));
    }


    /**
     * 删除对象信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @OperLog(description = "libraryReader删除对象信息")
    public ResponseResult<String> delete(@RequestBody List<Long> ids) {
        try {
            libraryReaderService.removeByIds(ids);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 分页查询数据信息
     *
     * @param libraryReader
     * @param pagination
     * @return
     */
    @RequestMapping("/list")
    @OperLog(description = "libraryReader分页查询数据信息")
    public ResponseResult<IPage<LibraryReader>> list(LibraryReader libraryReader, PaginAtion pagination) {
        Page page = pagination.getPage();
        return ResponseResult.success(libraryReaderService.page(page, new QueryWrapper<LibraryReader>(libraryReader)));
    }


    @RequestMapping("/all-list")
    @OperLog(description = "所有读者信息")
    public ResponseResult<List<LibraryReader>> allList(LibraryReader libraryReader) {
        return ResponseResult.success(libraryReaderService.list());
    }
}